//
//  MYGLikeListTableViewCell.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/18.
//

import UIKit

class MYGLikeListTableViewCell: UITableViewCell {
    var likeType = 0
    @IBOutlet weak var bView: UIView!
    @IBOutlet weak var timeLB: UILabel!
    @IBOutlet weak var subtitleLB: UILabel!
    @IBOutlet weak var icon: UIImageView!
    @IBOutlet weak var titleLB: UILabel!
    @IBOutlet weak var shareBtn: UIButton!
    var model = MYGTopicCollectionModel()
    override func awakeFromNib() {
        super.awakeFromNib()
        bView.layer.borderColor = UIColor.init(hexString: "#f1f1f1").withAlphaComponent(0.5).cgColor
        bView.layer.cornerRadius = 10
        bView.layer.borderWidth = 1
        bView.layer.masksToBounds = true
//        bView.addCorner(conrners: .allCorners, radius: 10, boradColor: UIColor.init(hexString: "#f1f1f1"), boradWidth: 1)
//        bView.addRoundedOrShadow(radius: 10, backGroundColor: .white, shadowOpacity: 5, shadowColor: UIColor.init(hexString: "#f1f1f1").withAlphaComponent(0.5))
    }

    @IBOutlet weak var likeBtn: UIButton!
    @IBOutlet weak var commentBtn: UIButton!
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @IBAction func onShare(_ sender: Any) {
    }
    @IBAction func onLike(_ sender: UIButton) {
        var likeStatus = "1"
        if self.model.is_like == 0 {
            likeStatus = "1"
            self.model.like = max(0, (self.model.like + 1))
            self.model.is_like = 1

        }else {
            likeStatus = "0"
            self.model.like = max(0, (self.model.like - 1))
            self.model.is_like = 0
        }
        updateLikeBtn()
        MYGNetwork<MYGMeAPI, MYGEmptyModel>.request(target: .likeSubmit(param: [
            "id":"\(self.model.id)",
           "like_type":"\(likeType)",
            "like_status":likeStatus])) { item in
               
       } failed: { _, error in
           MMYGHUD.showToast(text: error)
       }
    }
    
    @IBAction func onComment(_ sender: UIButton) {
    }
    
    func bindData(_ data: MYGTopicCollectionModel) {
        self.model = data
        self.titleLB.text = data.title
        self.subtitleLB.text = data.desc
        self.shareBtn.setTitle("\(data.share)", for: .normal)
        self.commentBtn.setTitle("\(data.comment)", for: .normal)
        self.likeBtn.setTitle("\(data.like)", for: .normal)
        self.timeLB.text = data.created_at.toTimeString()
        self.icon.sd_setImage(with: URL(string: data.img))
        updateLikeBtn()
    }
    func updateLikeBtn() {
        self.likeBtn.setTitle("\(self.model.like)", for: .normal)
        self.likeBtn.isSelected = self.model.is_like != 0
        self.likeBtn.setImage(self.model.is_like != 0 ? UIImage.init(named: "MYG_cloudDisk_liked") : UIImage.init(named: "MYG_cloudDisk_like_normal"), for: .selected)
    }
   
}
